package inet.db.tennis;
import java.math.BigDecimal;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Vector;

import inet.db.pool.*;
import inet.util.DateTime;
import inet.util.Logger;
import inet.util.Today;

public class TennisMatchDAO {

	private Logger logger = null;
	private DBPoolX poolX = null;

	public TennisMatchDAO() {
		logger = new Logger(this.getClass().getName());
		try {
			poolX = DBPoolX.getInstance(DBPoolXName.SERVICE_BONGDA);
		} catch (Exception exception) {
			logger.log(exception.toString());
		}
	}

	/**
	 * function get max stt of round of phase in a champion
	 * 
	 * @param phaseId
	 * @param round
	 * @return max stt
	 */
	public int getMaxSTT(BigDecimal phaseId, String round) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		int maxSTT = 0;

		try {
			conn = poolX.getConnection();
			strSQL = "select max(STT) from TENNIS_MATCH where PHASE_ID = ? and ROUND = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, phaseId);
			preStmt.setString(2, round);

			rs = preStmt.executeQuery();
			if (rs.next()) {
				maxSTT = rs.getInt(1);
			}
		} catch (SQLException e) {
			logger.error("getMaxSTT: Error executing SQL " + strSQL + ">>>"
					+ e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return maxSTT;
	}

	/**
	 * function get a tennis match
	 * 
	 * @param id
	 * @return TennisMatch
	 */
	public TennisMatch getRow(BigDecimal id) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		TennisMatch match = null;

		try {
			conn = poolX.getConnection();
			strSQL = " select a.ID, PLAYER_ID_1, PLAYER_ID_2, a.ROUND, MATCH_DATE, CHAMPION_CODE, MATCH_SCORE1, MATCH_SCORE2, a.STATUS, "
					+ " a.SEASON_ID, a.PHASE_ID, a.TOTAL_SET, a.STT, b.NAME, b.CODE_SMS, a.IS_MATCH_INV "
					+ " from TENNIS_MATCH a join TENNIS_CHAMPION b on a.CHAMPION_CODE = b.CODE "
					+ " where a.ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);

			rs = preStmt.executeQuery();
			if (rs.next()) {
				match = new TennisMatch();
				match.setId(rs.getBigDecimal(1));
				match.setFirstPlayerId(rs.getBigDecimal(2));
				match.setSecondPlayerId(rs.getBigDecimal(3));
				match.setRound(rs.getString(4));
				match.setMatchDate(rs.getTimestamp(5));
				match.setChampionCode(rs.getString(6));
				match.setFirstScore(rs.getInt(7));
				match.setSecondScore(rs.getInt(8));
				match.setStatus(rs.getInt(9));
				match.setSeasonId(rs.getBigDecimal(10));
				match.setPhaseId(rs.getBigDecimal(11));
				match.setTotalSet(rs.getInt(12));
				match.setStt(rs.getInt(13));
				match.setChampionName(rs.getString(14));
				match.setChampionCodeSMS(rs.getString(15));
				match.setIsMatchInv(rs.getInt(16));
			}
		} catch (SQLException e) {
			logger.error("getRow: Error executing SQL " + strSQL + ">>>" + e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return match;
	}
	
	/**
	 * function get current game id of match
	 * @param id
	 * @return BigDecimal
	 */
	public BigDecimal getCurrentGameId(BigDecimal id) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		BigDecimal currentGameId = BigDecimal.ZERO;
		
		try {
			conn = poolX.getConnection();
			strSQL = " select CURRENT_GAME from TENNIS_MATCH  " +
					 " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			
			rs = preStmt.executeQuery();
			if (rs.next()) {
				currentGameId = rs.getBigDecimal(1);
			}
		} catch (SQLException e) {
			logger.error("getCurrentGameId: Error executing SQL " + strSQL + ">>>" + e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return currentGameId;
	}
	
	/**
	 * function get result of match.
	 * @param id
	 * @return TennisMatch object.
	 */
	public TennisMatch getMatchResult(BigDecimal id) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		TennisMatch match = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " select a.ID, PLAYER_ID_1, PLAYER_ID_2, a.ROUND, MATCH_DATE, CHAMPION_CODE, MATCH_SCORE1, MATCH_SCORE2, a.STATUS, " +
					 " a.SEASON_ID, a.PHASE_ID, a.TOTAL_SET, a.STT, a.CURR_GAME_POINT1, a.CURR_GAME_POINT2, b.SCORE1, b.SCORE2,  " + 
					 " b.TIE_BREAK_SCORE1, b.TIE_BREAK_SCORE2, b.TIME_SET, b.VALUE, b.ID as M_SET_ID, a.CURRENT_GAME, a.CURRENT_SET, " +
					 " a.CURRENT_PLAYER_SERVE, a.MAP_MATCH_ID, b.STATUS " +
					 " from TENNIS_MATCH a left join TENNIS_SET b on a.ID = b.MATCH_ID " + 
					 " where a.ID = ? order by b.VALUE ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			
			rs = preStmt.executeQuery();
			BigDecimal matchId = BigDecimal.ZERO;
			int i = 1;
			while (rs.next()) {
				if(!matchId.equals(rs.getBigDecimal(1))) {
					i = 1; // Khởi tạo lại biến i
					match = new TennisMatch();
					match.setId(rs.getBigDecimal(1));
					match.setFirstPlayerId(rs.getBigDecimal(2));
					match.setSecondPlayerId(rs.getBigDecimal(3));
					match.setRound(rs.getString(4));
					match.setMatchDate(rs.getTimestamp(5));
					match.setChampionCode(rs.getString(6));
					match.setFirstScore(rs.getInt(7));
					match.setSecondScore(rs.getInt(8));
					match.setStatus(rs.getInt(9));
					match.setSeasonId(rs.getBigDecimal(10));
					match.setPhaseId(rs.getBigDecimal(11));
					match.setTotalSet(rs.getInt(12));
					match.setStt(rs.getInt(13));
					match.setCurrentGamePoint1(rs.getString(14));
					match.setCurrentGamePoint2(rs.getString(15));
					match.setCurrentGameId(rs.getBigDecimal("CURRENT_GAME"));
					match.setCurrentSet(rs.getInt("CURRENT_SET"));
					match.setCurrentPlayerServe(rs.getInt("CURRENT_PLAYER_SERVE"));
					match.setMapMatchId(rs.getBigDecimal("MAP_MATCH_ID"));
					matchId = rs.getBigDecimal(1);
				}
				//System.out.println(rs.getInt(16)+", "+ rs.getInt(17)+", "+rs.getInt(18)+", "+ rs.getInt(19)+", "+rs.getBigDecimal("M_SET_ID"));
				if(rs.getInt(21) == TennisSet.SET1) {
					match.setFirstSet1Score(rs.getInt(16));
					match.setSecondSet1Score(rs.getInt(17));
					match.setFirstTieBreakSet1Score(rs.getInt(18));
					match.setSecondTieBreakSet1Score(rs.getInt(19));
					match.setTimeSet1(rs.getInt(20));
					match.setSet1Id(rs.getBigDecimal("M_SET_ID"));
				} else if (rs.getInt(21) == TennisSet.SET2) {
					match.setFirstSet2Score(rs.getInt(16));
					match.setSecondSet2Score(rs.getInt(17));
					match.setFirstTieBreakSet2Score(rs.getInt(18));
					match.setSecondTieBreakSet2Score(rs.getInt(19));
					match.setTimeSet2(rs.getInt(20));
					match.setSet2Id(rs.getBigDecimal("M_SET_ID"));
				} else if (rs.getInt(21) == TennisSet.SET3) {
					match.setFirstSet3Score(rs.getInt(16));
					match.setSecondSet3Score(rs.getInt(17));
					match.setFirstTieBreakSet3Score(rs.getInt(18));
					match.setSecondTieBreakSet3Score(rs.getInt(19));
					match.setTimeSet3(rs.getInt(20));
					match.setSet3Id(rs.getBigDecimal("M_SET_ID"));
				} else if (rs.getInt(21) == TennisSet.SET4) {
					match.setFirstSet4Score(rs.getInt(16));
					match.setSecondSet4Score(rs.getInt(17));
					match.setFirstTieBreakSet4Score(rs.getInt(18));
					match.setSecondTieBreakSet4Score(rs.getInt(19));
					match.setTimeSet4(rs.getInt(20));
					match.setSet4Id(rs.getBigDecimal("M_SET_ID"));
				} else if (rs.getInt(21) == TennisSet.SET5) {
					match.setFirstSet5Score(rs.getInt(16));
					match.setSecondSet5Score(rs.getInt(17));
					match.setFirstTieBreakSet5Score(rs.getInt(18));
					match.setSecondTieBreakSet5Score(rs.getInt(19));
					match.setTimeSet5(rs.getInt(20));
					match.setSet5Id(rs.getBigDecimal("M_SET_ID"));
				}
			}
		} catch (SQLException e) {
			logger.error("getMatchResult: Error executing SQL " + strSQL + ">>>" + e.toString());
			e.printStackTrace();
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return match;
	}

	/**
	 * function get result of tennis match
	 * 
	 * @param id
	 * @return TennisMatch
	 */
	// public TennisMatch getMatchResult(BigDecimal id) {
	// Connection conn = null;
	// PreparedStatement preStmt = null;
	// ResultSet rs = null;
	// String strSQL = null;
	// TennisMatch match = null;
	//    	
	// try {
	// conn = poolX.getConnection();
	// strSQL =
	// " select ID, PLAYER_ID_1, PLAYER_ID_2, ROUND, MATCH_DATE, CHAMPION_CODE,
	// MATCH_SCORE1, MATCH_SCORE2, STATUS, TOTAL_SET, " +
	// " SET_1_SCORE1, SET_1_SCORE2, SET_2_SCORE1, SET_2_SCORE2, SET_3_SCORE1,
	// SET_3_SCORE2, SET_4_SCORE1, SET_4_SCORE2, SET_5_SCORE1, SET_5_SCORE2, " +
	// " TIE_BREAK_SET1_1, TIE_BREAK_SET1_2, TIE_BREAK_SET2_1 , TIE_BREAK_SET2_2
	// , TIE_BREAK_SET3_1, TIE_BREAK_SET3_2, TIE_BREAK_SET4_1, " +
	// " TIE_BREAK_SET4_2, TIE_BREAK_SET5_1, TIE_BREAK_SET5_2,
	// CURRENT_PLAYER_SERVE " +
	// " from TENNIS_MATCH " +
	// " where ID = ? ";
	// preStmt = conn.prepareStatement(strSQL);
	// preStmt.setBigDecimal(1, id);
	//    		
	// rs = preStmt.executeQuery();
	// if (rs.next()) {
	// match = new TennisMatch();
	// match.setId(rs.getBigDecimal(1));
	// match.setFirstPlayerId(rs.getBigDecimal(2));
	// match.setSecondPlayerId(rs.getBigDecimal(3));
	// match.setRound(rs.getString(4));
	// match.setMatchDate(rs.getTimestamp(5));
	// match.setChampionCode(rs.getString(6));
	// match.setFirstScore(rs.getInt(7));
	// match.setSecondScore(rs.getInt(8));
	// match.setStatus(rs.getInt(9));
	// match.setTotalSet(rs.getInt(10));
	// match.setFirstSet1Score(rs.getInt(11));
	// match.setSecondSet1Score(rs.getInt(12));
	// match.setFirstSet2Score(rs.getInt(13));
	// match.setSecondSet2Score(rs.getInt(14));
	// match.setFirstSet3Score(rs.getInt(15));
	// match.setSecondSet3Score(rs.getInt(16));
	// match.setFirstSet4Score(rs.getInt(17));
	// match.setSecondSet4Score(rs.getInt(18));
	// match.setFirstSet5Score(rs.getInt(19));
	// match.setSecondSet5Score(rs.getInt(20));
	//    			
	// match.setFirstTieBreakSet1Score(rs.getInt(21));
	// match.setSecondTieBreakSet1Score(rs.getInt(22));
	// match.setFirstTieBreakSet2Score(rs.getInt(23));
	// match.setSecondTieBreakSet2Score(rs.getInt(24));
	// match.setFirstTieBreakSet3Score(rs.getInt(25));
	// match.setSecondTieBreakSet3Score(rs.getInt(26));
	// match.setFirstTieBreakSet4Score(rs.getInt(27));
	// match.setSecondTieBreakSet4Score(rs.getInt(28));
	// match.setFirstTieBreakSet5Score(rs.getInt(29));
	// match.setSecondTieBreakSet5Score(rs.getInt(30));
	// match.setCurrentPlayerServe(rs.getBigDecimal(31));
	//    			
	// }
	// }
	// catch (SQLException e) {
	// logger.error("getMatchResult: Error executing SQL " + strSQL + ">>>" +
	// e.toString());
	// } finally {
	// poolX.releaseConnection(conn, preStmt, rs);
	// }
	// return match;
	// }
	//    
	/**
	 * function get list match from 10h59 today to 10h59 tomorrow
	 * 
	 * @param championCode
	 * @param round
	 * @param dd
	 * @param mm
	 * @param yyyy
	 * @param orderChampion
	 * @return Vector<TennisMatch>
	 */
	public Vector<TennisMatch> findMatchList(String championCode, String round,
			String dd, String mm, String yyyy, boolean orderChampion) {
		if (championCode == null)
			championCode = "%";
		else
			championCode = championCode.toUpperCase();

		if (round == null)
			round = "%";
		DateTime today = new DateTime();
		if (dd != null && dd.length() > 0)
			today.setDay(Integer.parseInt(dd));
		if (mm != null && mm.length() > 0)
			today.setMonth(Integer.parseInt(mm));
		if (yyyy != null && yyyy.length() > 0)
			today.setYear(Integer.parseInt(yyyy));
		today.setHour(10); // 10h59
		today.setMinute(59);

		DateTime tomorrow = today.nextDay();

		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		TennisMatch match = null;
		Vector<TennisMatch> vMatch = null;

		try {
			conn = poolX.getConnection();
			strSQL = " select a.ID, PLAYER_ID_1, PLAYER_ID_2, a.ROUND, MATCH_DATE, CHAMPION_CODE, MATCH_SCORE1, MATCH_SCORE2, a.STATUS, "
					+ " b.NAME, b.CODE_SMS, a.IS_MATCH_INV "
					+ " from TENNIS_MATCH a join TENNIS_CHAMPION b on a.CHAMPION_CODE = b.CODE "
					+ " where CHAMPION_CODE like ? AND ROUND like ? AND MATCH_DATE > ? AND MATCH_DATE < ? ";
			if (orderChampion) {
				strSQL += " order by b.NAME, CHAMPION_CODE, MATCH_DATE ";
			} else {
				strSQL += " order by MATCH_DATE ";
			}

			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, championCode);
			preStmt.setString(2, round);
			preStmt.setTimestamp(3, today.getTimestamp());
			preStmt.setTimestamp(4, tomorrow.getTimestamp());

			rs = preStmt.executeQuery();
			vMatch = new Vector<TennisMatch>();
			while (rs.next()) {
				match = new TennisMatch();
				match.setId(rs.getBigDecimal(1));
				match.setFirstPlayerId(rs.getBigDecimal(2));
				match.setSecondPlayerId(rs.getBigDecimal(3));
				match.setRound(rs.getString(4));
				match.setMatchDate(rs.getTimestamp(5));
				match.setChampionCode(rs.getString(6));
				match.setFirstScore(rs.getInt(7));
				match.setSecondScore(rs.getInt(8));
				match.setStatus(rs.getInt(9));
				match.setChampionName(rs.getString(10));
				match.setChampionCodeSMS(rs.getString(11));
				match.setIsMatchInv(rs.getInt(12));

				vMatch.addElement(match);
			}
		} catch (SQLException e) {
			logger.error("findMatchList: Error executing SQL " + strSQL + ">>>" + e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return vMatch;
	}

	public Vector<TennisMatch> findMatchListNew(String championCode,
			String round, String dd, String mm, String yyyy,
			boolean orderChampion) {
		if (championCode == null)
			championCode = "%";
		else
			championCode = championCode.toUpperCase();

		if (round == null)
			round = "%";
		if (dd == null)
			dd = "%";
		else if (dd.length() == 1)
			dd = "0" + dd;
		if (mm == null)
			mm = "%";
		else if (mm.length() == 1)
			mm = "0" + mm;
		if (yyyy == null)
			yyyy = "" + Today.getInstance().getYear();

		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		TennisMatch match = null;
		Vector<TennisMatch> vMatch = null;

		try {
			conn = poolX.getConnection();
			strSQL = " select a.ID, PLAYER_ID_1, PLAYER_ID_2, a.ROUND, MATCH_DATE, CHAMPION_CODE, MATCH_SCORE1, MATCH_SCORE2, a.STATUS, "
					+ " b.NAME, b.CODE_SMS, a.IS_MATCH_INV "
					+ " from TENNIS_MATCH a join TENNIS_CHAMPION b on a.CHAMPION_CODE = b.CODE "
					+ " where CHAMPION_CODE like ? and ROUND like ? and (TO_CHAR(MATCH_DATE, 'dd') like ? ) and (TO_CHAR(MATCH_DATE, 'mm') like ?) and (TO_CHAR(MATCH_DATE, 'yyyy') like ?) ";
			if (orderChampion) {
				strSQL += " order by b.NAME, CHAMPION_CODE, MATCH_DATE ";
			} else {
				strSQL += " order by MATCH_DATE ";
			}

			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, championCode);
			preStmt.setString(2, round);
			preStmt.setString(3, dd);
			preStmt.setString(4, mm);
			preStmt.setString(5, yyyy);

			rs = preStmt.executeQuery();
			vMatch = new Vector<TennisMatch>();
			while (rs.next()) {
				match = new TennisMatch();
				match.setId(rs.getBigDecimal(1));
				match.setFirstPlayerId(rs.getBigDecimal(2));
				match.setSecondPlayerId(rs.getBigDecimal(3));
				match.setRound(rs.getString(4));
				match.setMatchDate(rs.getTimestamp(5));
				match.setChampionCode(rs.getString(6));
				match.setFirstScore(rs.getInt(7));
				match.setSecondScore(rs.getInt(8));
				match.setStatus(rs.getInt(9));
				match.setChampionName(rs.getString(10));
				match.setChampionCodeSMS(rs.getString(11));
				match.setIsMatchInv(rs.getInt(12));

				vMatch.addElement(match);
			}
		} catch (SQLException e) {
			logger.error("findMatchList: Error executing SQL " + strSQL + ">>>"
					+ e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return vMatch;
	}

	/**
	 * function insert a tennis match
	 * 
	 * @param championCode
	 * @param player1
	 * @param player2
	 * @param round
	 * @param matchDate
	 * @param seasonId
	 * @param phaseId
	 * @param stt
	 * @param totalSet
	 * @return Id of match if insert success else return BigDecimal.Zero .
	 */
	public BigDecimal insertRow(String championCode, BigDecimal player1,
			BigDecimal player2, String round, Timestamp matchDate,
			BigDecimal seasonId, BigDecimal phaseId, int stt, int totalSet, int isMatchInv) {
		if (matchDate == null || championCode == null || player1 == null
				|| player2 == null) {
			return BigDecimal.ZERO;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		BigDecimal result = BigDecimal.ZERO;
		BigDecimal id = BigDecimal.ZERO;
		ResultSet rs = null;

		try {
			conn = poolX.getConnection();

			strSQL = "SELECT TENNIS_MATCH_SEQ.nextval FROM dual";
			preStmt = conn.prepareStatement(strSQL);
			rs = preStmt.executeQuery();
			if (rs.next()) {
				id = rs.getBigDecimal(1);
			}
			preStmt.close();

			strSQL = " insert into TENNIS_MATCH(ID, PLAYER_ID_1, PLAYER_ID_2, MATCH_DATE, ROUND, CHAMPION_CODE, "
					+ " SEASON_ID, PHASE_ID, STT, TOTAL_SET, IS_MATCH_INV) "
					+ " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			preStmt.setBigDecimal(2, player1);
			preStmt.setBigDecimal(3, player2);
			preStmt.setTimestamp(4, matchDate);
			preStmt.setString(5, round);
			preStmt.setString(6, championCode);
			preStmt.setBigDecimal(7, seasonId);
			preStmt.setBigDecimal(8, phaseId);
			preStmt.setInt(9, stt);
			preStmt.setInt(10, totalSet);
			preStmt.setInt(11, isMatchInv);

			if (preStmt.executeUpdate() >= 1) {
				result = id;
			}
		} catch (Exception ex) {
			logger.error("insertRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

	/**
	 * function update tennis match info
	 * 
	 * @param id
	 * @param round
	 * @param matchDate
	 * @param seasonId
	 * @param phaseId
	 * @param stt
	 * @param totalSet
	 * @return true if update successfull else return false.
	 */
	public boolean updateRow(BigDecimal id, String round, Timestamp matchDate,
			BigDecimal seasonId, BigDecimal phaseId, int stt, int totalSet) {
		if (matchDate == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;

		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set MATCH_DATE = ?, ROUND = ?, "
					+ " SEASON_ID = ?, PHASE_ID = ?, STT = ?, TOTAL_SET = ? "
					+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setTimestamp(1, matchDate);
			preStmt.setString(2, round);
			preStmt.setBigDecimal(3, seasonId);
			preStmt.setBigDecimal(4, phaseId);
			preStmt.setInt(5, stt);
			preStmt.setInt(6, totalSet);
			preStmt.setBigDecimal(7, id);

			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateRow: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

	/**
	 * function update score of match
	 * 
	 * @param match
	 * @return true if update successfull else return false.
	 */
	public boolean updateScore(TennisMatch match) {
		if (match == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;

		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set MATCH_SCORE1 = ?, MATCH_SCORE2 = ? "
					+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setInt(1, match.getFirstScore());
			preStmt.setInt(2, match.getSecondScore());
			preStmt.setBigDecimal(3, match.getId());

			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateScore: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update score of match
	 * @param id
	 * @param score1
	 * @param score2
	 * @return true if update success else return false.
	 */
	public boolean updateScore(BigDecimal id, int score1, int score2) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set MATCH_SCORE1 = ?, MATCH_SCORE2 = ?, LAST_UPDATED = sysdate "
				+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setInt(1, score1);
			preStmt.setInt(2, score2);
			preStmt.setBigDecimal(3, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateScore: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update score and status of match.
	 * @param id
	 * @param score1
	 * @param score2
	 * @param status
	 * @return true if update success else return false.
	 */
	public boolean updateScoreAndStatus(BigDecimal id, int score1, int score2, int status) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set MATCH_SCORE1 = ?, MATCH_SCORE2 = ?, STATUS = ?, LAST_UPDATED = sysdate "
				+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setInt(1, score1);
			preStmt.setInt(2, score2);
			preStmt.setInt(3, status);
			preStmt.setBigDecimal(4, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateScore: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update current point and currentServe of match
	 * @param id
	 * @param point1
	 * @param point2
	 * @param currentServe
	 * @return true if update success else return false.
	 */
	public boolean updateCurrentPointAndServe(BigDecimal id, String point1, String point2, int currentServe) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set CURR_GAME_POINT1 = ?, CURR_GAME_POINT2 = ?, CURRENT_PLAYER_SERVE = ?, LAST_UPDATED = sysdate "
				+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, point1);
			preStmt.setString(2, point2);
			preStmt.setInt(3, currentServe);
			preStmt.setBigDecimal(4, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateCurrentPointAndServe: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update current point, currentServe and status of match
	 * @param id
	 * @param point1
	 * @param point2
	 * @param currentServe
	 * @param status
	 * @return true if update success else return false.
	 */
	public boolean updateCurrentPointAndServe(BigDecimal id, String point1, String point2, int currentServe, int status) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set CURR_GAME_POINT1 = ?, CURR_GAME_POINT2 = ?, CURRENT_PLAYER_SERVE = ?, STATUS = ?, LAST_UPDATED = sysdate "
				+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, point1);
			preStmt.setString(2, point2);
			preStmt.setInt(3, currentServe);
			preStmt.setInt(4, status);
			preStmt.setBigDecimal(5, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateCurrentPointAndServe: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update current serve
	 * @param id
	 * @param currentServe
	 * @return true if update success else return false
	 */
	public boolean updateCurrentServe(BigDecimal id, int currentServe) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set CURRENT_PLAYER_SERVE = ?, LAST_UPDATED = sysdate "
				+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setInt(1, currentServe);
			preStmt.setBigDecimal(2, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateCurrentServe: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update match info
	 * @param id
	 * @param point1
	 * @param point2
	 * @param currentServe
	 * @param currentSet
	 * @param currentGameId
	 * @return true if update success else return false.
	 */
	public boolean updateCurrentInfo(BigDecimal id, String point1, String point2, int currentServe, int currentSet, BigDecimal currentGameId) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set CURR_GAME_POINT1 = ?, CURR_GAME_POINT2 = ?, CURRENT_PLAYER_SERVE = ?, " +
					 " CURRENT_SET = ?, CURRENT_GAME = ?, LAST_UPDATED = sysdate " +
				     " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, point1);
			preStmt.setString(2, point2);
			preStmt.setInt(3, currentServe);
			preStmt.setInt(4, currentSet);
			preStmt.setBigDecimal(5, currentGameId);
			preStmt.setBigDecimal(6, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateCurrentInfo: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update info of match.
	 * @param id
	 * @param score1
	 * @param score2
	 * @param point1
	 * @param point2
	 * @param currentServe
	 * @param currentSet
	 * @param status
	 * @param currentGameId
	 * @return true if update success else return false.
	 */
	public boolean updateCurrentInfo(BigDecimal id, int score1, int score2, String point1, String point2, int currentServe, int currentSet, int status, BigDecimal currentGameId) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set MATCH_SCORE1 = ?, MATCH_SCORE2 = ?, CURR_GAME_POINT1 = ?, CURR_GAME_POINT2 = ?, CURRENT_PLAYER_SERVE = ?, " +
					 " CURRENT_SET = ?, CURRENT_GAME = ?, STATUS = ?, LAST_UPDATED = sysdate " +
					 " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setInt(1, score1);
			preStmt.setInt(2, score2);
			preStmt.setString(3, point1);
			preStmt.setString(4, point2);
			preStmt.setInt(5, currentServe);
			preStmt.setInt(6, currentSet);
			preStmt.setBigDecimal(7, currentGameId);
			preStmt.setInt(8, status);
			preStmt.setBigDecimal(9, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateCurrentInfo: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

	/**
	 * function update status of match.
	 * 
	 * @param id
	 * @param status
	 * @return true if update successfull else return false.
	 */
	public boolean updateStatus(BigDecimal id, int status) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;

		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set STATUS = ? " + " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setInt(1, status);
			preStmt.setBigDecimal(2, id);

			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateScore: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

	/**
	 * function update value a field
	 * @param listId
	 * @param listValue
	 * @param field
	 * @param isNumber: true if update value Integer else update value String pass false.
	 * @return true if update success else return false.
	 */
	public boolean updateField(String[] listId, String[] listValue, String field, boolean isNumber) {
        if (listId == null || listValue == null) {
            logger.log("updateField: list is null --> could not be updated");
            return false;
        }
        int n = 0;
        if(listId.length < listValue.length){
     	   n = listId.length;
        } else{
     	   n = listValue.length;
        }       
        boolean result = false;
        Connection conn = null;
        PreparedStatement preStmt = null;
        String strSQL = null;
        int countSuccess = 0;
        try {
            conn = inet.db.pool.DBPool.getConnection();
            for(int i=0; i<n; i++){
 	           strSQL =
 	               "update TENNIS_MATCH set "+field+" = ?, LAST_UPDATED = sysdate " +
 	               " WHERE ID = ? ";
 	           preStmt = conn.prepareStatement(strSQL);
 	           if(isNumber){
 	        	  preStmt.setInt(1, Integer.parseInt(listValue[i].trim()));
 	           }else{
 	        	  preStmt.setString(1, listValue[i].trim());
 	           }
 	           preStmt.setBigDecimal(2, new BigDecimal(listId[i]));
 	           countSuccess = countSuccess + preStmt.executeUpdate();
 	           preStmt.close();
            }
            if(countSuccess > 0) result = true;
            
        } catch (SQLException e) {
            logger.error("updateField: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt);
        }
        return result;
    }
	
	public boolean updateSTT(int id, int stt) {

		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;

		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set STT = ? " + " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setInt(1, stt);
			preStmt.setInt(2, id);

			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateScore: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update first player id
	 * @param id
	 * @param playerId
	 * @return true if update success else return false.
	 */
	public boolean updateFirstPlayerId(BigDecimal id, BigDecimal playerId) {
		if(id == null || playerId == null) return false;
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set PLAYER_ID_1 = ? where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, playerId);
			preStmt.setBigDecimal(2, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateFirstPlayerId: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}
	
	/**
	 * function update second player id
	 * @param id
	 * @param playerId
	 * @return true if update success else return false.
	 */
	public boolean updateSecondPlayerId(BigDecimal id, BigDecimal playerId) {
		if(id == null || playerId == null) return false;
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set PLAYER_ID_2 = ? where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, playerId);
			preStmt.setBigDecimal(2, id);
			
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateSecondPlayerId: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

	/**
	 * function delete a TennisMatch
	 * 
	 * @param id
	 * @return true if update success else return false.
	 */
	public boolean deleteRow(BigDecimal id) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		boolean result = false;
		String strSQL = null;
		try {
			conn = poolX.getConnection();
			strSQL = " delete from TENNIS_MATCH where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("deleteRow: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt);
		}
		return result;
	}

	/**
	 * function get list match from 10h59 today to 10h59 tomorrow
	 * 
	 * @param championCode
	 * @param round
	 * @param dd
	 * @param mm
	 * @param yyyy
	 * @param orderChampion
	 * @return Vector<TennisMatch>
	 */

	public Vector<TennisMatch> findMatchListSchedule(String championCode,
			String round, int phaseId) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		TennisMatch match = null;
		String connd = "";

		if (championCode != null && !championCode.equals("")) {
			championCode = championCode.toUpperCase();
			connd += " And  upper(a.CHAMPION_CODE) =  '" + championCode + "'";
		}
		if (round != null && !round.equals("0")) {
			round = round.toUpperCase();
			connd += " AND upper(a.ROUND) like  '%" + round + "%'";
		} else {
			connd += " ";
		}

		if (phaseId != 0) {
			connd += " AND a.PHASE_ID =" + phaseId;
		} else {
			connd += " ";
		}

		Vector<TennisMatch> vMatch = null;
		try {
			conn = poolX.getConnection();
			strSQL = " select a.ID, PLAYER_ID_1, PLAYER_ID_2, a.ROUND, MATCH_DATE, CHAMPION_CODE, MATCH_SCORE1, MATCH_SCORE2, a.STATUS, "
					+ " b.NAME, b.CODE_SMS ,a.STT,a.PHASE_ID "
					+ " from TENNIS_MATCH a join TENNIS_CHAMPION b on a.CHAMPION_CODE = b.CODE "
					+ " where 1=1 " + connd + " order by a.STT asc ";
			preStmt = conn.prepareStatement(strSQL);
			rs = preStmt.executeQuery();
			vMatch = new Vector<TennisMatch>();
			while (rs.next()) {
				match = new TennisMatch();
				match.setId(rs.getBigDecimal(1));
				match.setFirstPlayerId(rs.getBigDecimal(2));
				match.setSecondPlayerId(rs.getBigDecimal(3));
				match.setRound(rs.getString(4));
				match.setMatchDate(rs.getTimestamp(5));
				match.setChampionCode(rs.getString(6));
				match.setFirstScore(rs.getInt(7));
				match.setSecondScore(rs.getInt(8));
				match.setStatus(rs.getInt(9));
				match.setChampionName(rs.getString(10));
				match.setChampionCodeSMS(rs.getString(11));
				match.setStt(rs.getInt(12));
				match.setPhaseId(rs.getBigDecimal(13));

				vMatch.addElement(match);
			}
		} catch (SQLException e) {
			logger.error("findMatchList: Error executing SQL " + strSQL + ">>>"
					+ e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return vMatch;
	}

	/**
	 * function get list match in a round of a phase of champion 
	 * @param championCode
	 * @param round
	 * @param phaseId
	 * @return Vector<TennisMatch> if has list match else return null;
	 */
	public Vector<TennisMatch> findMatchListInRoundOfChampion(String championCode, String round, BigDecimal phaseId) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		TennisMatch match = null;

		Vector<TennisMatch> vMatch = null;
		try {
			conn = poolX.getConnection();
			strSQL = " select a.ID, PLAYER_ID_1, PLAYER_ID_2, a.ROUND, MATCH_DATE, CHAMPION_CODE, MATCH_SCORE1, MATCH_SCORE2, a.STATUS, "
					+ " b.NAME, b.CODE_SMS ,a.STT, a.PHASE_ID, a.MAP_MATCH_ID, a.IS_MATCH_INV "
					+ " from TENNIS_MATCH a join TENNIS_CHAMPION b on a.CHAMPION_CODE = b.CODE "
					+ " where upper(a.CHAMPION_CODE) = upper(?) and upper(a.ROUND) like upper(?) and a.PHASE_ID = ? order by a.STT asc ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, championCode);
			preStmt.setString(2, "%"+round+"%");
			preStmt.setBigDecimal(3, phaseId);
			
			rs = preStmt.executeQuery();
			vMatch = new Vector<TennisMatch>();
			while (rs.next()) {
				match = new TennisMatch();
				match.setId(rs.getBigDecimal(1));
				match.setFirstPlayerId(rs.getBigDecimal(2));
				match.setSecondPlayerId(rs.getBigDecimal(3));
				match.setRound(rs.getString(4));
				match.setMatchDate(rs.getTimestamp(5));
				match.setChampionCode(rs.getString(6));
				match.setFirstScore(rs.getInt(7));
				match.setSecondScore(rs.getInt(8));
				match.setStatus(rs.getInt(9));
				match.setChampionName(rs.getString(10));
				match.setChampionCodeSMS(rs.getString(11));
				match.setStt(rs.getInt(12));
				match.setPhaseId(rs.getBigDecimal(13));
				match.setMapMatchId(rs.getBigDecimal(14));
				match.setIsMatchInv(rs.getInt(15));
				
				vMatch.addElement(match);
			}
		} catch (SQLException e) {
			logger.error("findMatchListInRoundOfChampion: Error executing SQL " + strSQL + ">>>" + e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return vMatch;
	}

	/**
	 * function get count match in round of champion
	 * @param championCode
	 * @param round
	 * @param phaseId
	 * @return total match in round of champion
	 */
	public int countMatchInRoundOfChampion(String championCode, String round, BigDecimal phaseId) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		String strSQL = null;
		int count = 0;

		try {
			conn = poolX.getConnection();
			strSQL = " select count(a.ID) "
					+ " from TENNIS_MATCH a "
					+ " where upper(a.CHAMPION_CODE) = upper(?) and upper(a.ROUND) like upper(?) and a.PHASE_ID = ? order by a.STT asc ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, championCode);
			preStmt.setString(2, "%"+round+"%");
			preStmt.setBigDecimal(3, phaseId);
			
			rs = preStmt.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			logger.error("countMatchInRoundOfChampion: Error executing SQL " + strSQL + ">>>" + e.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return count;
	}
	
	/**
	 * function update tennis match info
	 * 
	 * @param mapMatchId
	 *            Author : Hoannd
	 */
	public boolean updateRowByMapMatchId(BigDecimal id, BigDecimal mapMatchId) {
		if (mapMatchId == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;

		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set MAP_MATCH_ID = ?"
					+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			preStmt.setBigDecimal(2, mapMatchId);

			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateRow: Error executing SQL " + strSQL + ">>>"
					+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

	/**
	 * function insert a tennis match
	 * 
	 * @param championCode
	 * @param player1
	 * @param player2
	 * @param round
	 * @param matchDate
	 * @param seasonId
	 * @param phaseId
	 * @param stt
	 * @param totalSet
	 * @return Id of match if insert success else return BigDecimal.Zero .
	 */
	public BigDecimal insertRowTennisMatch(TennisMatch tennisMatch) {
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		BigDecimal result = BigDecimal.ZERO;
		BigDecimal id = BigDecimal.ZERO;
		ResultSet rs = null;
		try {

			conn = poolX.getConnection();
			strSQL = "SELECT TENNIS_MATCH_SEQ.nextval FROM dual";
			preStmt = conn.prepareStatement(strSQL);
			rs = preStmt.executeQuery();
			if (rs.next()) {
				id = rs.getBigDecimal(1);
			}
			preStmt.close();
			strSQL = " insert into TENNIS_MATCH(ID, PLAYER_ID_1, PLAYER_ID_2, MATCH_DATE, ROUND, CHAMPION_CODE, "
					+ "  PHASE_ID, STT) "
					+ " values (?,0,0, '01-Jan-09', ?, ?, ?,? )";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			preStmt.setString(2, tennisMatch.getRound());
			preStmt.setString(3, tennisMatch.getChampionCode());
			preStmt.setBigDecimal(4, tennisMatch.getPhaseId());
			preStmt.setInt(5, tennisMatch.getStt());

			if (preStmt.executeUpdate() >= 1) {
				result = id;
			}
		} catch (Exception ex) {
			logger.error("insertRow: Error executing SQL " + strSQL + ">>>"+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

	/**
	 * function update Map_Match_ID of match.
	 * 
	 * @param id
	 * @param status
	 * @return true if update successfull else return false. Author : Hoannd
	 */
	public boolean updateMapMatchId(BigDecimal id, BigDecimal mapMatchId) {
		if (id == null) {
			return false;
		}
		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		boolean result = false;
		ResultSet rs = null;
		System.out.println(id);
		try {
			conn = poolX.getConnection();
			strSQL = " update TENNIS_MATCH set MAP_MATCH_ID = ? "
					+ " where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, mapMatchId);
			preStmt.setBigDecimal(2, id);

			if (preStmt.executeUpdate() > 0) {
				result = true;
			}
		} catch (Exception ex) {
			logger.error("updateScore: Error executing SQL " + strSQL + ">>>"+ ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt, rs);
		}
		return result;
	}

}
